// pages/pay/pay.js

import config from './../../config';
import {
    empty,
    message,
    session,
    getOpenId
} from './../../utils/helper';
import {
    submitAdvisory,
    getPhoneNumber,
    wxPayment
} from './../../services/pay';
import qs from 'qs';

//获取应用实例
const app = getApp();
//实例化本地缓存
const cookie = new session();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        disabled: false,
    },

    //父级页面传递过来的表单参数
    params: {},

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        //显示当前页面的转发按钮
        wx.showShareMenu({
            withShareTicket: true
        });
        if (empty(options)) {
            console.warn('同学：你这个样子直接打开，是木有办法提交的表单的！');
        } else {
            this.params = Object.assign(options, qs.parse('body=' + options.body));
        }
    },

    //免费咨询表单提交
    submitForm(e) {
        let params = e.detail.value,
            that = this;
        if (!empty(params)) {
            params = Object.assign(this.params, params);
        } else {
            message('请选择套餐')
            return false;
        }
        this.setData({
            disabled: true,
        });
        submitAdvisory(params, {
            success: (res) => {
                wxPayment(res.data, {
                    success: () => {
                        wx.showModal({
                            showCancel: false,
                            content: '支付成功',
                            success: function(res) {
                                if (res.confirm) {
                                    wx.navigateTo({
                                        url: '/pages/my_ask/my_ask'
                                    });
                                }
                            }
                        })
                    },
                    fail: () => {
                        message('支付失败,请重试')
                    },
                    complete: () => {
                        that.setData({
                            disabled: false
                        });
                    }
                });
            },
            fail: (res) => {
                that.setData({
                    disabled: false
                })
                message(res.msg)
            }
        });
    },

    //下拉刷新事件
    onPullDownRefresh() {
        this.onLoad(this.params);
        wx.stopPullDownRefresh();
    },
})